#ifndef VARIAVEIS_
#define VARIAVEIS_

/*****************************************************************************
/ Avaliable commands
/----------------------------------------------------------------------------*/
/* It is assumed that SOM and EOM values do not occurin the message */
#define SOM  0xFD /* start of message */
#define EOM  0xFE /* end of message */
#define RCLK 0xC0 /* readclock */
#define SCLK 0XC1 /* set clock*/
#define RTL  0XC2 /* read temperature and luminosity */
#define RPAR 0XC3 /* read parameters */
#define MPMN 0XC4 /* modify monitoring period */
#define MTIN 0XC5 /* modify inactivity time */
#define RALA 0XC6 /* read alarms (clock, temp., luminosity, active/inactive) */
#define DALA 0XC7 /* define alarm clock*/
#define DALT 0XC8 /* define alarm temperature */
#define DALL 0XC9 /* define alarm luminosity */
#define AALA 0XCA /* activate/deactivate alarms */
#define IREG 0XCB /* information about registers (NREG, nr, iread, iwrite)*/
#define TRGC 0XCC /* transfer registers (curr. position)*/
#define TRGI 0XCD /* transfer registers (index) */
#define NMCH  0xCD  /* notificacao memoria cheia */

#define CMD_OK       0 /* command successful */
#define CMD_ERROR 0xFF /* error in command */

/****************************************************************************/
// Definicao de eventos comandados a partir da USART para registro na EEPROM
#define EINI 1 // inicio de execucao
#define ECLK 2 // acerto do relogio
#define EDALA 3 // definicao de alarme relogio
#define EDALT 4 // definicao de alarme temperatura
#define EDALL 5 // definicao de alarme luminosidade
#define EAALS 6 // activar/desactivar alarmes
#define EACLK 7 // alarme relogio
#define EALT 8 // alarme temperatura
#define EALL 9 // alarme luminosidade
#define EMPMN 10 // modificacao PMON
#define ENMCH 11 // notificacao memoria cheia

/******************************************/
//definicao de estados de funcionamento
#define NORMAL 0 //estado de funcionamento normal 
#define CONFIG_TIME_H 1 //configuracao das horas do relogio
#define CONFIG_TIME_M1 2 //configuracao dos minutos do relogio (dezenas)
#define CONFIG_TIME_M2 3 //configuracao dos minutos do relogio (unidades)
#define CONFIG_TIME_S1 4 //configuracao dos segundos do relogio (dezenas)
#define CONFIG_TIME_S2 5 //configuracao dos segundos do relogio (unidades)
#define CONFIG_ALARM_A 6 //opcao configuracao da hora do alarme
#define CONFIG_ALARM_A_H 7 //configuracao da hora do alarme (horas)
#define CONFIG_ALARM_A_M1 8 //configuracao da hora do alarme (dezenas minutos)
#define CONFIG_ALARM_A_M2 9 //configuracao da hora do alarme (unidades minutos)
#define CONFIG_ALARM_A_S1 10 //configuracao da hora do alarme (dezenas segundos)
#define CONFIG_ALARM_A_S2 11 //configuracao da hora do alarme (unidades segundos)
#define CONFIG_ALARM_T 12 //opcao de configuracao do limiar de temperatura 
#define CONFIG_ALARM_T_V1 13 //configuracao do limiar de temperatura (dezenas)
#define CONFIG_ALARM_T_V2 14 //configuracao do limiar de temperatura (unidades)
#define CONFIG_ALARM_L 15 //opcao de configuracao do limiar de luminosidade
#define CONFIG_ALARM_L_V 16 //configuracao do limiar de luminosidade
#define CONFIG_ALARMS 17 //toggle da activacao de alarmes
#define CONFIG_ENERGY 18 //toggle do modo de poupanca de energia

#define NUMBER_OF_STATES 19 

/******************************************/
#define REC_SIZE 13
#define CHECKSUM_NUMBER 0xAA

/******************************************/

typedef struct {
	unsigned alarms_enabled:1;
	unsigned energy_save_enabled:1;
	unsigned alarm_clock:1; //time alarm flag
	unsigned alarm_temp:1; //temp alarm flag
	unsigned alarm_light:1; //light alarm flag
	unsigned half_mem:1; //indicacao da ocupacao de metade da memoria
} status_flags;

extern status_flags flags;

/******************************************/

extern unsigned char PMON; //periodo entre monitorizacoes
extern unsigned char TSOM; //tempo de alarme sonoro
extern unsigned char NREG; //numero maximo de registos no buffer circular da eeprom

extern unsigned char nr; //numero de registos validos
extern unsigned char iescrita; //indice escrita
extern unsigned char ileitura; //indice leitura

/******************************************/
// current status

typedef struct {
	unsigned char state; //estado de funcionamento actual

	unsigned char time_h; //horas do relogio
	unsigned char time_m; //minutos do relogio
	unsigned char time_s; //segundos do relogio

	unsigned char light; //valor da ultima medicao de iluminacao
	unsigned char temp; //valor da ultima medicao de temperatura

	unsigned char alarm_on; //tempo restante (seg) de alarme sonoro
	
	unsigned char time_to_mon; //segundos ate a proxima leitura de sensores
}status_vars;

extern status_vars status;

/******************************************/
//Alarm settings

typedef struct{
	unsigned char time_h; //hora de alarme (horas)
	unsigned char time_m; //hora de alarme (minutos)
	unsigned char time_s; //hora de alarme (segundos)

	unsigned char temp; //valor de disparo da temperatura
	unsigned char light;//valor de disparo da iluminacao
}alarm_settings;

extern alarm_settings alarm;

/******************************************/
extern unsigned char ee_read_pos;
extern unsigned char ee_write_pos;

/******************************************/	
extern unsigned char termina_conf ;//verifica se terminou configuracao

/*************************/
extern unsigned char mod_r, mod_a, mod_t, mod_l, mod_A;

extern unsigned char registro[8];

extern alarm_settings aux;

#endif